var {Card} = require("Poker")


var Direction = cc.Enum({
    Right: 0,
    Left: 1,
})

var Conver = cc.Enum({
    None: 0,
    First: 1,
    All: 2
})

let CardPool = null


cc.Class({
    extends: cc.Component,

    properties: {
        cardPrefab: cc.Prefab,
        direction: {
            type: Direction,
            default: Direction.Left
        },
        conver: {
            type: Conver,
            default: Conver.First
        },
        offset: 0,
        isMe: false

    },
    // use this for initialization
    onLoad: function () {
        this.cards = new Array()
    },

    add(cardId) {
        let card = Card.fromId(cardId)
        let count = this.node.getChildren().length
        this.cards.push(card)
        let tempCard = cc.instantiate(this.cardPrefab)
        let width = tempCard.width
        let x = 0

        switch (this.direction) {
            case Direction.Left:
                x = -(count - 1) * this.offset
                tempCard.zIndex = 10 - count
                break
            default:
                x = (count - 1) * this.offset
                tempCard.zIndex = count
                break
        }
        console.log(x)
        tempCard.setPosition(x, 0, 0)
        let card_entry = tempCard.getComponent('CardView')
        card_entry.card = card
        if (!this.isMe) {
            if (Conver.All == this.conver || count == 0 && this.conver == Conver.First) {
                card_entry.flipBack()
            }
        }
        this.node.addChild(tempCard)
    },

    changeCard(cardIndex){

    },

    flip() {
        let cardNodes = this.node.getChildren()
        for (let card of cardNodes) {
            let card_entry = card.getComponent('CardView')
            card_entry.flipFront()
        }
    },

    clear() {
        this.cards = new Array()
        let cardsNode = this.node.getChildren()
        for (let item of cardsNode) {
            let remove = cc.removeSelf(true)
            item.runAction(remove)
        }
    },
});
